package com.cryptoneedle.center.source.service;

import com.cryptoneedle.center.config.helper.ConfigPropertyHelper;
import com.cryptoneedle.center.source.dolphin.response.Project;
import com.cryptoneedle.center.source.dolphin.response.Result;
import com.cryptoneedle.center.source.service.crud.*;
import com.cryptoneedle.center.source.util.DsUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-10-23
 */
@Slf4j
@Service
@Transactional
public class DolphinSchedulerService {

    @Resource
    private DolphinSchedulerService dolphinSchedulerService;

    @Autowired
    private FoundDataService select;
    @Autowired
    private AddDataService add;
    @Autowired
    private PatchDataService patch;
    @Autowired
    private SaveDataService save;
    @Autowired
    private DeleteDataService delete;

    @Resource
    private JdbcTemplate dolphinJdbcTemplate;

    @Autowired
    private RestTemplate restTemplate;

    public List<Project> projects() {
        List<Project> projects = dolphinJdbcTemplate.query("select * from t_ds_project", new BeanPropertyRowMapper<>(Project.class));
        return projects;
    }

    public void createProject() {
        String projectName = ConfigPropertyHelper.dsProjectName();
        List<Project> projects = dolphinSchedulerService.projects();
        if (projects.isEmpty() || projects.stream().noneMatch(project -> projectName.equals(project.getName()))) {
            String url = ConfigPropertyHelper.dsUrl() + "projects?projectName=" + projectName + "&description=【⚠️自动任务，禁止操作⚠️】";
            restTemplate.postForObject(url, DsUtil.httpEntity(), Result.class);
        }
    }

    public void onlineDsWorkflow(String catalogName, String databaseName, String tableName) {

    }

    public void taskDefinition(String catalogName, String databaseName, String tableName) {
        // 查询DS项目

        // 查询工作流

        // 更新工作流
    }
}